<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewExchangeRate extends JView
{
	function display($tpl = null) {

		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		// Make sure you are logged in and have the necessary access rights
		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'exchangerate', 'manage' )) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		if( $this->getLayout() == 'form' ) {
			$this->_displayForm($tpl);
			return;
		} 

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//Get data form the model
		$model	= $this->getModel();
		$row	= $this->get('Data');
		$profile= $model->getUser($user->get('id'));

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEdit	= ($user->gid < 25)? Ownservice::checkAccess('exchangerate','edit','exchangerate','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_EXCHANGERATE_DETAIL'));
		$document->setTitle( $params->get('page_title') );

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('access', $access);
		$this->assignRef('profile', $profile);

		parent::display($tpl);
	}

	function _displayForm() {
		
		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$gid		= $user->get('gid');
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//Get data form the model
		$model			= $this->getModel();
		$row			= & $this->get('Data');
		$currencies		= & $this->get('Currencies');
		$branches		= & $this->get('Branches');

		// check if it's an edit action
		if($row->id) {
				$canEdit	= ($gid<25)?Ownservice::checkAccess('exchangerate', 'edit', 'exchangerate', 'all'):1;
				if (!$canEdit) {
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
				}
		} else {
				$canAdd		= ($gid<25)?Ownservice::checkAccess('exchangerate', 'add', 'exchangerate', 'all'):1;
				if ( !$canAdd) {
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
				}
		}

		//set page title
		if($row->id) {
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_EXCHANGERATE'));
		} else {
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_EXCHANGERATE'));
		}
		$document->setTitle( $params->get('page_title') );

		$lists = array();
		
		$lists['branch'] 	= OwnserviceHelper::buildbranchesselect($branches, 'branch_id', $row->branch_id, true, 'class="required" style="width:250px;"');
		
		$lists['currency'] 	= OWnserviceHelper::buildcurrenciesselect($currencies, 'currency_id', $row->currency_id, true, 'class="required" style="width:250px;"'); 
		
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('lists', $lists);

		parent::display($tpl);
	}
}
